package com.seeyon.apps.ebs.controller;

import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.ebs.dao.AddNewEnumDao;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.controller.BaseController;
import com.seeyon.ctp.common.ctpenumnew.manager.EnumManager;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.common.po.ctpenumnew.CtpEnumItem;
import com.seeyon.ctp.util.UUIDLong;
import org.apache.commons.logging.Log;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

public class AddNewEnumController extends BaseController {

    private static final Log log = CtpLogFactory.getLog(AddNewEnumController.class);
    private EnumManager enumManager = (EnumManager) AppContext.getBean("enumManagerNew");
    private AddNewEnumDao addNewEnumDao = (AddNewEnumDao) AppContext.getBean("addNewEnumDao");
    public void addNewEnum(HttpServletRequest request, HttpServletResponse response){
        response.setCharacterEncoding("utf-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
            String data = request.getParameter("enumDetail");
            Long parentEnumId = -1196471532917930187L;    //2155569630491659896
            Map<String, Object> maxEnumSort = addNewEnumDao.getMaxEnumSort(parentEnumId);
            log.info("maxEnumSort:"+maxEnumSort);
            if (maxEnumSort != null&& !maxEnumSort.isEmpty()) {
                int enumvalue = Integer.parseInt(String.valueOf(maxEnumSort.get("enumvalue")))+1;
                Long sortnumber = Long.parseLong(String.valueOf(maxEnumSort.get("sortnumber")))+1;
                //获取特定父级枚举
                CtpEnumItem enumItem = new CtpEnumItem();
                enumItem.setId(UUIDLong.longUUID());
                enumItem.setEnumvalue(""+enumvalue);
                enumItem.setSortnumber(sortnumber);
                enumItem.setShowvalue(data);
                enumItem.setIfuse("N");
                enumItem.setRefEnumid(parentEnumId);
                enumItem.setParentId(0L);
                enumItem.setRootId(0L);
                enumItem.setLevelNum(0);
                enumItem.setOutputSwitch(1);
                enumItem.setOrgAccountId(670869647114347L);  //1020592944117532241
                enumItem.setState(1);
                enumManager.saveEnumItem(parentEnumId,2,enumItem);
                log.info("enumItem:"+enumItem.toJSON());
                //访问其他接口
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("success",true);
                writer.write(jsonObject.toString());
            }
        } catch (IOException | BusinessException e) {
            log.error("获取需要字段失败",e);
        } finally {
            if (writer!=null){
                writer.close();
            }
        }
    }

    public ModelAndView showAddEnum(HttpServletRequest request, HttpServletResponse response){
        ModelAndView view = new ModelAndView();
        //设置跳转页面
        view.setViewName("ebs/showAddEnum");
        return view;
    }


}
